package com.zzyl.controller.customer;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.ReservationDto;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountVo;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/customer/reservation")
public class CustomerReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Integer> getCancelledReservationCount() {
        //获取当前用户的userid
        Long userId = UserThreadLocal.getUserId();
        //根据当前用户的id查询取消预约的次数
        Integer count = reservationService.getCancelledCount(userId);
        return success(count);
    }

    /**
     * 新增预约
     *
     * @param dto
     * @return
     */
    @PostMapping
    @ApiOperation("新增预约")
    public ResponseResult insertReservation(@RequestBody ReservationDto dto) {
        reservationService.insertReservation(dto);
        return success();
    }

    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @GetMapping("countByTime")
    @ApiOperation("查询每个时间段剩余预约次数")
    public ResponseResult<List<CountVo>> countByTime(@RequestParam("time") Long time) {
        List<CountVo> countVo = reservationService.countByTime(time);
        return success(countVo);
    }

    /**
     * 分页查询预约信息
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @GetMapping("/page")
    public ResponseResult<PageResponse> selectByPage(
            @RequestParam(name = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
            @RequestParam(name = "status",required = false)Integer status
    ){
            PageResponse<ReservationVo> page =  reservationService.selectByPage(pageNum,pageSize,status);
            return ResponseResult.success(page);
    }

    /**
     * 取消预约
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public ResponseResult cancelReservation(@PathVariable("id") Integer id) {
        reservationService.cancelReservation(id);
        return success();
    }

}
